package com.mxpio.erp.technology.controller;


import com.mxpio.erp.common.technology.entity.ProcessGroupLine;
import com.mxpio.erp.common.technology.service.ProcessGroupLineService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProcessGroupLineController", description = "工序段行信息接口")
@RestController
@RequestMapping("/erp/tech/procgroupline/")
public class ProcessGroupLineController {

    @Autowired
    private ProcessGroupLineService service;

    @GetMapping("list")
    @Operation(summary = "工序段行列表", description = "获取工序段行列表（无分页）", method = "GET")
    public Result<List<ProcessGroupLine>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProcessGroupLine> lines = service.list(ProcessGroupLine.class, criteria);
        return Result.OK(lines);
    }

    @GetMapping("page")
    @Operation(summary = "工序段行列表", description = "获取工序段行列表（分页）", method = "GET")
    public Result<Page<ProcessGroupLine>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProcessGroupLine> lines = service.listPage(ProcessGroupLine.class, page, criteria);
        return Result.OK(lines);
    }

    @GetMapping("list/{procGroupCode}")
    @Operation(summary = "根据procGroupCode获取工序段行", description = "根据procGroupCode获取工序段行", method = "GET")
    public Result<List<ProcessGroupLine>> listByBizNo(@Parameter(description="业务单号") @PathVariable(name = "procGroupCode", required = true) String procGroupCode) {
        Criteria c = Criteria.create().addCriterion("procGroupCode", Operator.EQ, procGroupCode);
        List<ProcessGroupLine> lines = service.list(c);
        if (lines.size()>0){
            lines.forEach(per -> {
                per.setSaveTransient(false);
                per.getProcessInfo().setSaveTransient(false);
            });//save时 不需要处理明细行上关联的主单对象
        }
        return Result.OK(lines);
    }

/*    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除工序段行", description = "删除工序段行,多个id用英文逗号分隔,procGroupCode和lineNo间使用英文冒号分隔,procGroupCode在前,lineNo在后", method = "DELETE")
    public Result<ProcessGroupLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            String[] pk = key.split(":");
            OrderLineKey lineKey = new OrderLineKey();
            lineKey.setBizNo(pk[0]);
            lineKey.setLineNo(pk[1]);
            service.remove(ProcessGroupLine.class, lineKey);
        }
        return Result.OK();
    }*/

}
